package com.imooc.user.service;

import com.imooc.enums.YesOrNo;
import com.imooc.user.mapper.UserAddressMapper;
import com.imooc.user.pojo.UserAddress;
import com.imooc.user.pojo.bo.AddressBo;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @Author: 顾志杰
 * @Date: 2019/11/13
 * @Time: 15:12
 */
@RestController
public class AddressServiceImpl implements AddressService {

    private final UserAddressMapper userAddressMapper;
    private final Sid sid;

    @Autowired
    public AddressServiceImpl(UserAddressMapper userAddressMapper, Sid sid) {
        this.userAddressMapper = userAddressMapper;
        this.sid = sid;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<UserAddress> queryAll(String userId) {
        UserAddress ua = new UserAddress();
        ua.setUserId(userId);
        return userAddressMapper.select(ua);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void addNewUserAddress(AddressBo addressBo) {
        //判断当前用户是否存在地址，如果没有，则设为默认地址
        Integer isDefault = 0;
        List<UserAddress> userAddresses = this.queryAll(addressBo.getUserId());
        if (userAddresses.size() == 0) {
            isDefault = 1;
        }
        //保存
        UserAddress newAddress = new UserAddress();
        /*如果两边的字段名是一样的可以直接赋值*/
        BeanUtils.copyProperties(addressBo, newAddress);
        newAddress.setId(sid.nextShort());
        newAddress.setIsDefault(isDefault);
        newAddress.setCreatedTime(new Date());
        newAddress.setUpdatedTime(new Date());
        userAddressMapper.insert(newAddress);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void updateUserAddress(AddressBo addressBo) {
        String addressId = addressBo.getAddressId();
        UserAddress updateAddress = new UserAddress();
        BeanUtils.copyProperties(addressBo, updateAddress);
        updateAddress.setId(addressId);
        updateAddress.setUpdatedTime(new Date());
//        userAddressMapper.updateByPrimaryKey(updateAddress);  //null的属性会覆盖到数据库数据
        userAddressMapper.updateByPrimaryKeySelective(updateAddress);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void deleteUserAddress(String userid, String addressId) {
        UserAddress deleteAddress = new UserAddress();
        deleteAddress.setUserId(userid);
        deleteAddress.setId(addressId);
        userAddressMapper.delete(deleteAddress);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void setDefalutUserAddress(String userid, String addressId) {
        // 1.查找默认地址设置为不默认
        UserAddress queryAddress = new UserAddress();
        queryAddress.setUserId(userid);
        queryAddress.setIsDefault(YesOrNo.YES.type);
        List<UserAddress> userAddress = userAddressMapper.select(queryAddress);
        userAddress.forEach(a -> {
            a.setIsDefault(YesOrNo.NO.type);
            userAddressMapper.updateByPrimaryKeySelective(a);
        });
        // 2.根据地址id设置默认地址
        UserAddress defaultAddress = new UserAddress();
        defaultAddress.setId(addressId);
        defaultAddress.setUserId(userid);
        defaultAddress.setIsDefault(YesOrNo.YES.type);
        userAddressMapper.updateByPrimaryKeySelective(defaultAddress);
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public UserAddress queryUserAddress(String userId, String addressId) {
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        userAddress.setId(addressId);
        return userAddressMapper.selectOne(userAddress);
    }
}
